package smarture;

import jnibwapi.BWAPIEventListener;
import jnibwapi.JNIBWAPI;
import jnibwapi.model.Unit;
import jnibwapi.types.UnitType;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.util.HashSet;

/**
 * Created by Stefan Rudolph on 17.02.14.
 */
public class SmartureAI implements BWAPIEventListener, Runnable {

    private final JNIBWAPI bwapi;

    private Vulture myUnit;

    private HashSet<Unit> enemyUnits;

    private int frame;
    
    private boolean dead = false;

    public SmartureAI() {
        System.out.println("This is the SmartureAI LCS! :)");

        bwapi = new JNIBWAPI(this, false);
    }

    public static void main(String[] args) {
        new SmartureAI().run();
    }

    @Override
    public void matchStart() {
        enemyUnits = new HashSet<>();

        frame = 0;

        bwapi.enablePerfectInformation();
        bwapi.enableUserInput();
        bwapi.setGameSpeed(0);
        
        bwapi.drawHealth(true); 
        bwapi.drawTargets(true);
    }

    @Override
    public void matchFrame() {
    	if( myUnit != null && !dead )
        	myUnit.step();

        if (frame % 1000 == 0) {
            System.out.println("Frame: " + frame);
        }
        frame++;
    }

    @Override
    public void unitDiscover(int unitID) {
    	Unit unit = bwapi.getUnit(unitID);
        int typeID = unit.getTypeID();

        if (typeID == UnitType.UnitTypes.Terran_Vulture.getID()) {
            if (unit.getPlayerID() == bwapi.getSelf().getID()) {
            	dead = false;
            	myUnit = new Vulture(unit, bwapi, enemyUnits);
            } else {
                enemyUnits.add(unit);
            }
        } else if (typeID == UnitType.UnitTypes.Protoss_Zealot.getID()) {
            if (unit.getPlayerID() != bwapi.getSelf().getID()) {
                enemyUnits.add(unit);
            }
        }
    }

    @Override
    public void unitDestroy(int unitID) {
    	if( myUnit != null && unitID == myUnit.getID() )
    	{
    		dead = true;
    		return;
    	}
       
        Unit rmUnit = null;
        for (Unit u : enemyUnits) {
            if (u.getID() == unitID) {
                rmUnit = u;
                break;
            }
        }
        if( rmUnit != null )
        {
        	enemyUnits.remove(rmUnit);
	        System.out.println("Enemies left: "+enemyUnits.size());
        }
    }

    @Override
    public void connected() {
        System.out.println("Connected");
    }

    @Override
    public void matchEnd(boolean winner) {
    	myUnit.saveLCS();
    	// save results
    	try
    	{
    	    //create a temporary file
    	    File logFile=new File("results.txt");

    	    BufferedWriter writer = new BufferedWriter(new FileWriter(logFile,true));
    	    
    	    String s = new String();
    	    if( dead )
    	    	s += ""+0+" ";
    	    else
    	    	s += ""+myUnit.getHealth()+ " ";
    	    
    	    int sum = 0;
    	    for(Unit u : enemyUnits)
    	    {
    	    	sum += u.getHitPoints();
    	    }
    	    s += ""+sum+" "+winner+"\n";
    	    
    	    writer.write (s);

    	    //Close writer
    	    writer.close();
    	} catch(Exception e)
    	{
    	    e.printStackTrace();
    	}
    	System.out.println("Won: "+winner);
    }

    @Override
    public void keyPressed(int keyCode) {

    }

    @Override
    public void sendText(String text) {

    }

    @Override
    public void receiveText(String text) {

    }

    @Override
    public void playerLeft(int playerID) {

    }

    @Override
    public void nukeDetect(int x, int y) {

    }

    @Override
    public void nukeDetect() {

    }

    @Override
    public void unitEvade(int unitID) {

    }

    @Override
    public void unitShow(int unitID) {

    }

    @Override
    public void unitHide(int unitID) {

    }

    @Override
    public void unitCreate(int unitID) {
    }

    @Override
    public void unitMorph(int unitID) {

    }

    @Override
    public void unitRenegade(int unitID) {

    }

    @Override
    public void saveGame(String gameName) {

    }

    @Override
    public void unitComplete(int unitID) {

    }

    @Override
    public void playerDropped(int playerID) {

    }

    @Override
    public void run() {
        bwapi.start();
    }
}


